package com.xiaoye.qbproject.controller.qbzt;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.SubjectZb;
import com.xiaoye.qbproject.domain.ZbInfo;
import com.xiaoye.qbproject.page.TableDataInfo;
import com.xiaoye.qbproject.service.SubjectZbService;
import com.xiaoye.qbproject.vo.SubjectZbVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("subjectZb")
public class SubjectZbController extends BaseController {

    @Autowired
    private SubjectZbService subjectZbService;

    /**
     * @param subjectZbVo
     * @description: 获取 专题对应的装备列表
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.page.TableDataInfo
     * @ceateDate: 2025/2/11 下午4:48
     */

    @RequestMapping("getZbListOfSubjectZb")
    public TableDataInfo getZbListOfSubjectZb(SubjectZbVo subjectZbVo) {
        startPage();
        List<ZbInfo> zbInfos = subjectZbService.getZbListOfSubjectZb(subjectZbVo);
        return getDataTable(zbInfos);
    }

    @PostMapping()
    public AjaxResult addSubjectZb(@RequestBody SubjectZbVo subjectZbVo) {
        List<String> zbIds = subjectZbVo.getZbIds();
        if (zbIds == null || zbIds.isEmpty()) {
            return error("请选择装备");
        }
        String subjectId = subjectZbVo.getSubjectId();
        if (subjectId == null || subjectId.isEmpty()) {
            return error("请选择专题");
        }
        subjectZbService.addSubjectZb(subjectZbVo);
        return success();
    }

    @DeleteMapping
    public AjaxResult removeSubjectZb(@RequestBody SubjectZbVo subjectZbVo) {
        if (subjectZbVo.getSubjectId() == null || subjectZbVo.getSubjectId().isEmpty()) {
            return error("请选择专题");
        }
        if (subjectZbVo.getZbIds() == null || subjectZbVo.getZbIds().isEmpty()) {
            return error("请选择装备");
        }
        subjectZbService.remove(Wrappers.lambdaQuery(SubjectZb.class)
                .eq(SubjectZb::getSubjectId, subjectZbVo.getSubjectId())
                .in(SubjectZb::getZbId, subjectZbVo.getZbIds())
        );
        return success();
    }
}